<template>
  <div class="app-viewer">
    
    <div class="viewer-header">
      <!-- <el-button
            plain
            icon="el-icon-arrow-left"
            style="margin-bottom: 20px;"
            type="text"
            @click="doBack">
            返 回
        </el-button> -->
      <el-row>
        <el-col :span="12">
          <el-row :gutter="10" class="mb20">
            <el-col :span="1.5">
              <el-button
                type="primary"
                @click="handleAdd"
                v-hasPermi="['data:info:add']"
              >
                新增
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                plain
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['data:info:remove']"
              >
                删除
              </el-button>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="12" style="text-align: right">
          <transition name="fade">
            <span v-show="!isShowQuery" style="margin-right: 10px">
              <el-input
                v-model="queryParams.searchValue"
                placeholder="请输入搜索关键字"
                style="width: 200px; margin-right: 10px"
              ></el-input>
              <el-button type="primary" @click="handleQuery"> 查询 </el-button>
              <el-button type="primary" @click="resetQuery"> 重置 </el-button>
            </span>
          </transition>
          <el-button type="primary" @click="switchQuery">
            {{ isShowQuery ? "普通检索" : "高级检索" }}
          </el-button>
        </el-col>
      </el-row>
      <el-collapse-transition>
        <div class="query" v-show="isShowQuery">
          <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
            label-width="100px"
            @submit.native.prevent
          >
            <el-form-item prop="sensorid">
              <el-input
                v-model="queryParams.sensorid"
                placeholder="请输入传感器id"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item prop="sensortemp">
              <el-input
                v-model="queryParams.sensortemp"
                placeholder="请输入传感器温度"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item prop="sensorvoltage">
              <el-input
                v-model="queryParams.sensorvoltage"
                placeholder="请输入电压值"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item prop="ip">
              <el-input
                v-model="queryParams.ip"
                placeholder="请输入ip地址"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item prop="time">
              <el-date-picker
                clearable
                size="small"
                v-model="queryParams.time"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择接收数据时间"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleQuery">搜索</el-button>
              <el-button type="primary" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-collapse-transition>
    </div>

    <div class="viewer-body">
      <el-table
        v-loading="loading"
        :data="infoList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="传感器id" align="center" prop="sensorid" />
        <el-table-column label="传感器温度" align="center" prop="sensortemp" />
        <el-table-column label="电压值" align="center" prop="sensorvoltage" />
        <el-table-column label="ip地址" align="center" prop="ip" />
        <el-table-column
          label="接收数据时间"
          align="center"
          prop="time"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.time, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="备注"
          align="center"
          prop="remark"
          show-overflow-tooltip
        />
        <!-- <el-table-column label="跳转功能" align="center" width="180">
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        @click="toLink('跳转页面链接',{pid:scope.row.id})">
                        {{scope.row.显示数字}}
                    </el-button>
                </template>
            </el-table-column> -->
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['data:info:edit']"
            >
              修改
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 添加或修改历史数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" class="open-container">
      <el-form
        :inline="true"
        ref="form"
        :model="form"
        :rules="rules"
        label-width="140px"
        @submit.native.prevent
      >
        <el-form-item label="传感器id" prop="sensorid">
          <el-input
            v-model="form.sensorid"
            placeholder="请输入传感器id"
            style="width: 300px"
          />
        </el-form-item>
        <el-form-item label="传感器温度" prop="sensortemp">
          <el-input
            v-model="form.sensortemp"
            placeholder="请输入传感器温度"
            style="width: 300px"
          />
        </el-form-item>
        <el-form-item label="电压值" prop="sensorvoltage">
          <el-input
            v-model="form.sensorvoltage"
            placeholder="请输入电压值"
            style="width: 300px"
          />
        </el-form-item>
        <el-form-item label="ip地址" prop="ip">
          <el-input
            v-model="form.ip"
            placeholder="请输入ip地址"
            style="width: 300px"
          />
        </el-form-item>
        <el-form-item label="接收数据时间" prop="time">
          <el-date-picker
            clearable
            size="small"
            v-model="form.time"
            type="date"
            style="width: 300px"
            value-format="yyyy-MM-dd"
            placeholder="选择接收数据时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
            style="width: 750px"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInfo,
  getInfo,
  delInfo,
  addInfo,
  updateInfo,
} from "@/api/data/info";

export default {
  name: "Info",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 历史数据表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue: "",
        sensorid: null,
        sensortemp: null,
        sensorvoltage: null,
        ip: null,
        time: null,
      },
      isShowQuery: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 跳转其他页面 */
    toLink(link, params) {
      this.$router.push({
        path: link,
        query: params,
      });
    },
    /** 返回上一页 */
    doBack() {
      this.$router.go(-1);
    },
    /** 查询历史数据列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then((response) => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sensorid: null,
        sensortemp: null,
        sensorvoltage: null,
        ip: null,
        time: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.searchValue = "";
      this.handleQuery();
    },
    switchQuery() {
      this.isShowQuery = !this.isShowQuery;
      this.resetForm("queryForm");
      this.queryParams.searchValue = "";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加历史数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改历史数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除选择的历史数据数据项？")
        .then(function () {
          return delInfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "data/info/export",
        {
          ...this.queryParams,
        },
        `info_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped>
.query {
  border-top: 1px solid #dfe6ec;
  border-bottom: 1px solid #dfe6ec;
  text-align: right;
}
.query >>> .el-form--inline .el-form-item {
  margin-right: 0;
  margin-left: 10px;
  margin-bottom: 14px;
  margin-top: 14px;
}
.app-container >>> .el-button--text:hover {
  border-color: #0000;
  background: #0000;
}
</style>
